home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.09 Sep 96 / C++ DebugStr / debugbuf.h < prev    next >
Encoding:
Text File  |  1996-06-18  |  3.8 KB  |  142 lines  |  [TEXT/R*ch]

  1. // (c) copyright 1995,1996, Jon Kalb, Liberty Software
  2. // Kalb@LibertySoft.com
  3. // You may freely incorporate this code into any machine-
  4. // readable project. You may modify this code, but you may
  5. // not remove this statement. 
  6.  
  7. #ifndef _DEBUGBUF_
  8. #define _DEBUGBUF_
  9.  
  10. #include <Types.h>    // for "true" and "false"
  11.  
  12. class debugbuf: public streambuf
  13. {
  14.     public:
  15.  
  16.         //debugbuf();
  17.         //virtual ~debugbuf();
  18.         // Construction and destruction are really handled
  19.         // by class debugbufinit. Initialization is done
  20.         // in the private routine init().
  21.  
  22.     protected:
  23.  
  24.         // These protected member functions are virtual
  25.         // functions in the parent (streambuf) class that
  26.         // we override to create our behavior.
  27.  
  28.         // The name overflow may be confusing--this
  29.         // just outputs a character to the stream.
  30.         // Calls outputchar().
  31.         virtual int overflow(int c = EOF);
  32.  
  33.         // Since this is not an input stream, we want both
  34.         // pbackfail() and underflow() to return EOF (thus
  35.         // indicating failure). It turns out that the
  36.         // default behavior (the base class implementation)
  37.         // does just that.
  38.         //virtual int pbackfail(int c = EOF);
  39.         //virtual int underflow();
  40.  
  41.         // we don't do input so always return EOF
  42.         virtual int uflow() {return EOF;}
  43.  
  44.         // we don't do input so always return 0 chars read
  45.         virtual int xsgetn(char *, int) {return 0;}
  46.  
  47.         // Calls outputchar() for each character.
  48.         virtual int xsputn(const char *s, int n);
  49.  
  50.         // We use the default behavior which returns
  51.         // a streampos that is in an invalid position.
  52.         // We do not support repositioning on this
  53.         // stream.
  54.         //virtual streampos seekoff(streamoff off,
  55.         //                            ios::seekdir way,
  56.         //                            ios::openmode which =
  57.         //                                ios::in | ios::out);
  58.         //virtual streampos seekpos(streampos sp,
  59.         //                            ios::openmode which =
  60.         //                                ios::in | ios::out);
  61.  
  62.         // We don't support setting the buffer so
  63.         // we use the default which is just to return
  64.         // "this."
  65.         //virtual streambuf *setbuf(char *s, int n);
  66.  
  67.         // There is nothing to sync with, so we
  68.         // just do the default which is to return
  69.         // zero, indicating no error.
  70.         //virtual int sync();
  71.         // Actually, as an alternative implementation it
  72.         // would be possible to use this function to call
  73.         // our soft flush routine. In practice, there would
  74.         // be no different result. sync() is usually only
  75.         // called by pubsync(), which is usually only called
  76.         // by flush(), which is usually only called by
  77.         // the endl manipulator function after it has
  78.         // streamed '\n'. So the soft flush would always
  79.         // follow a hard flush and result in a no-op.
  80.  
  81.     private:
  82.         enum
  83.         {
  84.             kMaxDebugStrReadableString = 60,
  85.             kSizeOfSemicolonG = 2,
  86.             kSizeOfLengthByte = 1,
  87.             kTabSize = 5,
  88.             kStop = true
  89.         };
  90.  
  91.         // the buffer
  92.         char pbeg[    kSizeOfLengthByte +
  93.                     kMaxDebugStrReadableString +
  94.                     kSizeOfSemicolonG];
  95.  
  96.         // location of the next streamed char
  97.         char *pnext;
  98.  
  99.         // This always points to the end of the readable
  100.         // string buffer. Once it is set in init(), it is
  101.         // never modified
  102.         char *pend;
  103.  
  104.         // the number of queued alerts
  105.         int alertCount;
  106.  
  107.         void init();
  108.  
  109.         void outputchar(char c);
  110.         void formfeed();
  111.         void horztab();
  112.         void backspace();
  113.         void verttab();
  114.         void alert();
  115.         void addchar(char c);
  116.         void softflush();
  117.         void flushalerts();
  118.         void flushdebugstring(int stop = false);
  119.  
  120.     friend class debugbufinit;
  121. };
  122.  
  123. class debugbufinit
  124. {
  125.         static unsigned int count;
  126.     public:
  127.         debugbufinit();
  128.  
  129.         // Our destructor is not virtual. It is important
  130.         // that objects of this class have no memory
  131.         // footprint. We will end up with one object of this
  132.         // class per translation unit (.cp file). If this
  133.         // class has any virtual member functions then
  134.         // objects of this class would have v tables in
  135.         // memory. Since this is not intended to be a base
  136.         // class for other class, there is no need to be
  137.         // virtual.
  138.         ~debugbufinit();
  139. };
  140.  
  141. #endif    // _DEBUGBUF_
  142.